package cc.rengu.igas.channel.alipay.biz.impl;

import cc.rengu.igas.channel.alipay.common.constant.AlipayServiceConstant;
import cc.rengu.igas.channel.alipay.common.enums.RespCodeEnum;
import cc.rengu.igas.channel.alipay.facade.AlipayPubService;
import cc.rengu.igas.channel.alipay.facade.request.AliGetAuthCodeRequest;
import cc.rengu.igas.channel.alipay.facade.request.AliGetUserIdRequest;
import cc.rengu.igas.channel.alipay.facade.request.IdcQueryRequest;
import cc.rengu.igas.channel.alipay.facade.request.ImageUploadRequest;
import cc.rengu.igas.channel.alipay.facade.response.AliGetAuthCodeResponse;
import cc.rengu.igas.channel.alipay.facade.response.AliGetUserIdResponse;
import cc.rengu.igas.channel.alipay.facade.response.IdcQueryResponse;
import cc.rengu.igas.channel.alipay.facade.response.ImageUploadResponse;
import cc.rengu.igas.channel.alipay.facade.result.Result;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.DubboResult;
import cc.rengu.oltp.service.realize.DubboService;
import cc.rengu.oltp.service.realize.impl.DubboServiceImpl;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

import java.io.ByteArrayOutputStream;
import java.io.PrintWriter;

public class AlipayPubServiceImpl implements AlipayPubService {
    private RgLogger rglog = RgLog.getLogger(this.getClass().getName());

    @Override
    public Result<AliGetAuthCodeResponse> aliGetAuthCode(AliGetAuthCodeRequest aliGetAuthCodeRequest) {
        DubboService dubboService = new DubboServiceImpl();
        Result<AliGetAuthCodeResponse> result = new Result<>();
        AliGetAuthCodeResponse getAuthCodeResponse = new AliGetAuthCodeResponse();
        try {
            DubboResult dubboResult = dubboService.callAtomService(aliGetAuthCodeRequest,
                    AlipayServiceConstant.AUTH_CODE_SERVICE, AliGetAuthCodeResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            result.setResult((AliGetAuthCodeResponse) dubboResult.getResult());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("获取用户授权失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                getAuthCodeResponse.setRespDesc(((BizException) e).getCode());
                getAuthCodeResponse.setRespDesc(e.getMessage());
            } else {
                getAuthCodeResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                getAuthCodeResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
            result.setResult(getAuthCodeResponse);
        }
        return result;
    }

    @Override
    public Result<AliGetUserIdResponse> aliGetUserId(AliGetUserIdRequest aliGetUserIdRequest) {
        DubboService dubboService = new DubboServiceImpl();
        Result<AliGetUserIdResponse> result = new Result<>();
        AliGetUserIdResponse getUserIdResponse = new AliGetUserIdResponse();
        try {
            DubboResult dubboResult = dubboService.callAtomService(aliGetUserIdRequest,
                    AlipayServiceConstant.GET_USERID_SERVICE, AliGetUserIdResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            result.setResult((AliGetUserIdResponse) dubboResult.getResult());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("获取支付宝用户id失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                getUserIdResponse.setRespDesc(((BizException) e).getCode());
                getUserIdResponse.setRespDesc(e.getMessage());
            } else {
                getUserIdResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                getUserIdResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
            result.setResult(getUserIdResponse);
        }
        return result;
    }

    @Override
    public Result<ImageUploadResponse> imageUpload(ImageUploadRequest imageUploadRequest) {
        DubboService dubboService = new DubboServiceImpl();
        Result<ImageUploadResponse> result = new Result<>();
        ImageUploadResponse imageUploadResponse = new ImageUploadResponse();
        try {
            DubboResult dubboResult = dubboService.callAtomService(imageUploadRequest,
                    AlipayServiceConstant.IMAGE_UPLOAD_SERVICE, ImageUploadResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            result.setResult((ImageUploadResponse) dubboResult.getResult());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("图片上传失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                imageUploadResponse.setRespDesc(((BizException) e).getCode());
                imageUploadResponse.setRespDesc(e.getMessage());
            } else {
                imageUploadResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                imageUploadResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
            result.setResult(imageUploadResponse);
        }
        return result;
    }

    @Override
    public Result<IdcQueryResponse> idcQuery(IdcQueryRequest idcQueryRequest) {
        DubboService dubboService = new DubboServiceImpl();
        Result<IdcQueryResponse> result = new Result<>();
        IdcQueryResponse idcQueryResponse = new IdcQueryResponse();
        try {
            DubboResult dubboResult = dubboService.callAtomService(idcQueryRequest,
                    AlipayServiceConstant.IDC_QUERY_SERVICE, IdcQueryResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            result.setResult((IdcQueryResponse) dubboResult.getResult());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("idc查询失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                idcQueryResponse.setRespDesc(((BizException) e).getCode());
                idcQueryResponse.setRespDesc(e.getMessage());
            } else {
                idcQueryResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                idcQueryResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
            result.setResult(idcQueryResponse);
        }
        return result;
    }
}
